/*******************************************************************************
 * Copyright (c) 2018 8 24.
 * Author: yoonasy
 */

/**
 * decodeURI 将URI（统一资源标识符）解码，如其中的URL统一资源定位符
 */
export default {
  /**
   * @Description 获取cookie
   * @param key
   */
  get (key) {
    function process (key) {
      if (!key) {
        return key
      }
      for (;key !== decodeURI(key);) {
        key = decodeURI(key)
      }
      let encode = ['<', '>', "'", '"', '%3c', '%3e', '%27', '%22', '%253c', '%253e', '%2527', '%2522']
      let e16 = ['&#x3c;', '&#x3e;', '&#x27;', '&#x22;', '%26%23x3c%3B', '%26%23x3e%3B', '%26%23x27%3B', '%26%23x22%3B', '%2526%2523x3c%253B', '%2526%2523x3e%253B', '%2526%2523x27%253B', '%2526%2523x22%253B']
      for (let i = 0; i < encode.length; i++) {
        key = key.replace(new RegExp(encode[i], 'gi'), e16[i])
      }
      return key
    }
    // 正则匹配key返回数组赋值给res
    let reg = RegExp('(^|;\\s*)' + key + '=([^;]*)(;|$)')
    let res
    let param = (res = document.cookie.match(reg)) ? decodeURI(res[2]) : ''
    return process(param)
  },
  /**
   * @Description   添加cookie四属性
   * @param key     cookie名
   * @param value   cookie值
   * @param domain  domain值
   * @param path    path值
   * @param expires 过期时间(小时)
   */
  set (key, value, domain, path, expires) {
    let time = new Date()
    if (expires) {
      time.setTime(time.getTime() + 36e5 * expires)
      // t + '=' + e + '; expires=' + p.toGMTString() + '; path=' + (n || '/') + '; ' + (i ? 'domain=' + i + ';' : ''))
      document.cookie = `${key}=${value}; expires=${time.toGMTString()}; path=${path || '/'};${domain ? ` domain=${domain};` : ''}`
    } else {
      // t + '=' + e + '; path=' + (n || '/') + '; ' + (i ? 'domain=' + i + ';' : '')
      document.cookie = `${key}=${value}; path=${path || '/'};${domain ? ` domain=${domain};` : ''}`
    }
  },
  /**
   * @Description  删除cookie
   * @param key
   * @param domain
   * @param path
   */
  del (key, domain, path) {
    // t + '=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path=' + (i || '/') + '; ' + (e ? 'domain=' + e + ';' : '')
    document.cookie = `${key}=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path=${path || '/'};${domain ? ` domain=${domain}` : ''}`
  },
  // /**
  //  * 删除Cookie
  //  * @param name
  //  */
  // del (name) {
  //   function setCookie (cname, cvalue, exdays) {
  //     var d = new Date()
  //     d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
  //     var expires = 'expires=' + d.toUTCString()
  //     document.cookie = cname + '=' + cvalue + '; ' + expires
  //     console.log(document.cookie)
  //   }
  //   setCookie(name, '', -1)
  // },
  /**
   * 转换成对象序列
   * @param callback
   */
  toObj (callback) {
    let temp = {}
    let val = document.cookie.match(/(^|;\s*)\S+(\w+)/g)
    val = val.map(e => (e.indexOf('; ') !== -1) ? e.substring(e.indexOf('; ') + 2) : e)
    val.forEach(e => { temp[e.split('=')[0]] = e.split('=')[1] })
    return temp
  },
  /**
   * 清空cookie
   */
  clear () {
    let v = this.toObj()
    for (let k in v) {
      if (v.hasOwnProperty(k)) {
        this.del(k)
      }
    }
  }
}
